package edu.hz.pro1_50;

public class Main0014 {


    public static void main(String[] args) {
        Main0014 main0014 = new Main0014();
        System.out.println(main0014.longestCommonPrefix(new String[]{"a", "abd", "afff"}).equals("a"));
        System.out.println(main0014.longestCommonPrefix(new String[]{"c", "abd", "afff"}).equals("a"));
        System.out.println(main0014.longestCommonPrefix(new String[]{"flower", "flow", "flight"}).equals("fl"));
    }

    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        if (strs.length == 1) {
            return strs[0];
        }
        for (int a = strs[0].length(); a > 0; a--) {
            boolean bre = false;
            for (int b = 1; b < strs.length; b++) {
                if (strs[b].length() < a + 1
                        || !strs[b].startsWith(strs[0].substring(0, a))) {
                    bre = true;
                    break;
                }
            }
            if (!bre && a != 0) {
                return strs[0].substring(0, a);
            }

        }
        return "";
    }
}
